package com.skins{
	import com._public._net.FaceLibrary;
	import com.skins.data.CssVo;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.media.Sound;
	import flash.text.TextFormat;

	/**
	 * 多皮肤库处理
	 * @author WZH(shch8.com)
	 */
	public class Librarys {
		public var libs:Array = [];
		public var imgDefaultPath:String;
		public var sheet:MkConfig = MkConfig.getInstance();

		public function addLib(value:FaceLibrary):void {
			libs.push(value);
		}

		public function clear():void {
			libs = [];
		}

		private function getLib(act:String, className:String):Object {
			var getObj:Object
			for (var i:Object in libs){
				if (libs[i] != null){
					getObj = libs[i][act](className);
					if (getObj != null){
						return getObj;
					}
				}
			}
			return null;
		}

		/**
		 * 获取复合式皮肤，皮肤从CSS中定义，可能是元件也可能是图片
		 * @param	cssName		样式名
		 * @param	classVar	样式ID
		 * @return
		 */
		public function getSkin(cssName:String, classVar:String):DisplayObject {
			var className:String = sheet.getStyle(cssName)[classVar];
			if (className == null){
				//trace("未找到样式:",cssName,classVar)
				return null;
			}
			if (className.indexOf(CssVo.DIS_ITEM) == -1)
				return getSprite(className);
			var display:DisplayObject = new ImgSkinLoader(className, imgDefaultPath);
			return display;
		}

		/**
		 * CSS文字样式获取，支持获取复合样式
		 * @param	cssName		样式名
		 * @param	classVar	复合样式中的复合属性名，空值表示非复合样式
		 * @return
		 */
		public function getTextFormat(cssName:String, classVar:String = ""):TextFormat {
			var obj:Object = getClassObj(cssName, classVar);
			if (obj == null){
				trace("未找到字体样式:", cssName + "." + classVar);
				return new TextFormat;
			}
			return sheet.transform(obj);
		}

		/**
		 * CSS样式获取，支持获取复合样式
		 * @param	cssName		样式名
		 * @param	classVar	复合样式中的复合属性名，空值表示非复合样式
		 * @return
		 */
		public function getClassObj(cssName:String, classVar:String = ""):Object {
			if (classVar == "")
				return sheet.getStyle(cssName);
			var classValue:String = getClassValue(cssName, classVar)
			if (classValue == null)
				return null;
			return sheet.getStyle(classValue);
		}

		/**
		 * 获取CSS复合样式值
		 * @param	cssName		样式名
		 * @param	classVar	复合样式中的复合属性名，空值表示非复合样式
		 * @return
		 */
		public function getClassValue(cssName:String, classVar:String = ""):String {
			return sheet.getStyleValue(cssName, classVar)
		}

		/**
		 * Button类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getButton(className:String):SimpleButton {
			return getLib("getButton", className) as SimpleButton;
		}

		/**
		 * Sprite类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getSprite(className:String):Sprite {
			return getLib("getSprite", className) as Sprite;
		}

		/**
		 * MovieClip类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getMovieClip(className:String):MovieClip {
			return getLib("getMovieClip", className) as MovieClip;
		}

		/**
		 * Sound类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getSound(className:String):Sound {
			return getLib("getSound", className) as Sound;
		}

		/**
		 * 图片类型方式获取
		 * @param	className 类名
		 * @return
		 */
		public function getImage(className:String):BitmapData {
			return getLib("getImage", className) as BitmapData;
		}
	}
}